package com.coffee.order.service.sku.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.sku.query.SkuOrderDeliveryDemandQuery;
import com.coffee.order.api.sku.result.SkuOrderDeliveryDemandResult;
import com.coffee.order.api.sku.service.SkuOrderDeliveryDemandService;
import com.coffee.order.service.sku.bean.SkuOrderDeliveryDemand;
import com.coffee.order.service.sku.dao.SkuOrderDeliveryDemandDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class SkuOrderDeliveryDemandServiceImpl implements SkuOrderDeliveryDemandService {
	@Resource
	private SkuOrderDeliveryDemandDao	skuOrderDeliveryDemandDao;

	@Override
	public Page<SkuOrderDeliveryDemandResult> getPager(SkuOrderDeliveryDemandQuery param, PagerInfo pr) {
		Page<SkuOrderDeliveryDemandResult> pager=new Page<SkuOrderDeliveryDemandResult>(pr.getStart(), pr.getPageSize());
		Integer count=skuOrderDeliveryDemandDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<SkuOrderDeliveryDemand> list=skuOrderDeliveryDemandDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, SkuOrderDeliveryDemandResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(SkuOrderDeliveryDemandQuery param){
		Integer count=skuOrderDeliveryDemandDao.count(param);
		return count;
	}
	
	@Override
	public SkuOrderDeliveryDemandResult getDetail(Integer id){
		SkuOrderDeliveryDemand item=skuOrderDeliveryDemandDao.findById(id);
		if(item==null){
			return null;
		}
		SkuOrderDeliveryDemandResult result=new SkuOrderDeliveryDemandResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<SkuOrderDeliveryDemandResult> getList(SkuOrderDeliveryDemandQuery param, PagerInfo pr){
		List<SkuOrderDeliveryDemand> list=skuOrderDeliveryDemandDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, SkuOrderDeliveryDemandResult.class);
	}
	
	@Override
	public List<SkuOrderDeliveryDemandResult> getList(SkuOrderDeliveryDemandQuery param){
		List<SkuOrderDeliveryDemand> list=skuOrderDeliveryDemandDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SkuOrderDeliveryDemandResult.class);
	}
	
	@Override
	public List<SkuOrderDeliveryDemandResult> getList(){
		SkuOrderDeliveryDemandQuery param = new SkuOrderDeliveryDemandQuery();
		List<SkuOrderDeliveryDemand> list=skuOrderDeliveryDemandDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SkuOrderDeliveryDemandResult.class);
	}


	@Override
	public void delete(Integer id) {
		skuOrderDeliveryDemandDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(SkuOrderDeliveryDemandResult result)throws BusinessException {
		SkuOrderDeliveryDemand item=new SkuOrderDeliveryDemand();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			skuOrderDeliveryDemandDao.insert(item);
		}else {
			SkuOrderDeliveryDemand tmp=skuOrderDeliveryDemandDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			skuOrderDeliveryDemandDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,SkuOrderDeliveryDemandResult> getItemMap(Set<Integer> idSet){
		Map<Integer, SkuOrderDeliveryDemandResult> map=new HashMap<Integer, SkuOrderDeliveryDemandResult>();
		List<SkuOrderDeliveryDemand> list=skuOrderDeliveryDemandDao.findByIds(StringUtil.setToList(idSet));
		List<SkuOrderDeliveryDemandResult> resultList=DTOUtils.convertList(list, SkuOrderDeliveryDemandResult.class);
		for(SkuOrderDeliveryDemandResult item:resultList) {
			map.put(item.getPostageId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,SkuOrderDeliveryDemandResult> getItemMap(){
		Map<Integer, SkuOrderDeliveryDemandResult> map=new HashMap<Integer, SkuOrderDeliveryDemandResult>();
		SkuOrderDeliveryDemandQuery param=new SkuOrderDeliveryDemandQuery();
		List<SkuOrderDeliveryDemand> list=skuOrderDeliveryDemandDao.page(param, 0, Integer.MAX_VALUE);
		List<SkuOrderDeliveryDemandResult> resultList=DTOUtils.convertList(list, SkuOrderDeliveryDemandResult.class);
		for(SkuOrderDeliveryDemandResult item:resultList) {
			map.put(item.getPostageId(), item);
		}
		return map;
	}	
	
	
	
}
